using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace MusicTag
{

    class PlayList
    {
      private SortedDictionary<string,string> PList;
      private string ListName;

        public PlayList()
        {
            ListName = "New PlayList";
            PList = new SortedDictionary<string, string>();
        }


        public PlayList(string file)
        {
            PList = new SortedDictionary<string, string>();
            Load(file);
        }

        public void setName(string name)
        {
            ListName = name;
        }

        public string getName()
        {
            return ListName; 
        }

        public bool exists(string key)
        {
            if (PList.ContainsKey(key))
                return true;
            return false;
        }

        public SortedDictionary<string,string>.KeyCollection GetKeys()
        {
            return PList.Keys;

        }

        public string GetValue(string key)
        {
            return PList[key];
        }

        public void Clear()
        {
            PList.Clear();
        }

        public void Load(string filename)
        {
            StreamReader sr = new StreamReader(filename);
            string key, val;

            ListName = sr.ReadLine();
            while (sr.EndOfStream == false)
            {
                key = sr.ReadLine();
                val = sr.ReadLine();
                Add(key, val);
            }

            sr.Close();
        }

        public void Save(string filename)
        {
            StreamWriter sr = new StreamWriter(filename);
            string key, val;
            int i = 0;

            sr.WriteLine(ListName);
            KeyValuePair<string,string>[] pairs = new KeyValuePair<string,string>[PList.Count];
            PList.CopyTo(pairs,0);
            for(i = 0; i < pairs.Length; i++)
            {
                
                sr.WriteLine(pairs[i].Key);
                sr.WriteLine(pairs[i].Value);
            }

            sr.Close();
        }

        public bool Add(string shortname, string fullpath)
        {
            if(PList.ContainsKey(shortname) == false)
            {
                PList.Add(shortname, fullpath);
                return true;
            }
            else return false;

        }

        public bool Remove(string shortname)
        {
            if (PList.ContainsKey(shortname) == false)
            {
                PList.Remove(shortname);
                return true;
            }
            else return false;
        }

    }
}
